Day 25 시뮬레이션, 조건문, 수학
Day 단계 2023-11-15
1. 문자열 밀기
- 내 풀이 : StringBuilder를 사용해 A의 문자를 이동 및 재배열 시키고 B와 비교했다.
class Solution {
public int solution(String A, String B) {
StringBuilder sb = new StringBuilder();
int answer = 0;
if (B.equals(A)) return answer;
else {
for(int i = 0; i < A.length(); i++) {
sb.append(A.charAt(A.length()-1)).append(A.substring(0, A.length()-1));
A = sb.toString();
answer++;
if (!B.equals(A)) {
sb.setLength(0);
continue;
}
else return answer;
}
}
return -1;
}
}
- 다른 사람 풀이 : B를 2번 연속으로 붙인 문자열에서 A가 등장하는 index를 반환했다.
return (B+B).indexOf(A);